﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transport
{
    public class PhanCongControl
    {
        transportEntities db;

        public PhanCongControl()
        {
            db = new transportEntities();
        }

        public int count(string driverid)
        {
            return db.phan_cong.Where(t => t.ma_tai_xe == driverid).Count();
        }

        public bool checkCarStatus(string carnumber, DateTime begin, DateTime end)
        {
            try
            {
                List<phan_cong> ls = db.phan_cong.Where(t => t.bien_so_xe == carnumber).ToList();
                ChiTietDatXeControl ctrl = new ChiTietDatXeControl();
                
                foreach (phan_cong t in ls)
                {
                    chi_tiet_dat_xe value = ctrl.findById(t.ma_dat_xe);

                    if (value.tu_ngay <= begin && value.den_ngay >= begin || value.tu_ngay <= end && value.den_ngay >= end)
                        return false;
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool checkDriverStatus(string driverid, DateTime begin, DateTime end)
        {
            try
            {
                List<phan_cong> ls = db.phan_cong.Where(t => t.ma_tai_xe == driverid).ToList();
                ChiTietDatXeControl ctrl = new ChiTietDatXeControl();

                foreach (phan_cong t in ls)
                {
                    chi_tiet_dat_xe value = ctrl.findById(t.ma_dat_xe);

                    if (value.tu_ngay <= begin && value.den_ngay >= begin || value.tu_ngay <= end && value.den_ngay >= end)
                        return false;
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool insert(phan_cong value)
        {
            try
            {
                db.phan_cong.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(phan_cong value)
        {
            try
            {
                phan_cong bg = db.phan_cong.SingleOrDefault(t => t.bien_so_xe == value.bien_so_xe && t.ma_dat_xe == value.ma_dat_xe);
                bg = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string carnumber, string reservationid)
        {
            try
            {
                phan_cong bg = db.phan_cong.SingleOrDefault(t => t.bien_so_xe == carnumber && t.ma_dat_xe == reservationid);
                db.phan_cong.DeleteObject(bg);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}